package Reports;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Retail.RetailProduct;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;


public class ProductReport extends HttpServlet{

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req, resp);
	}
	
	@SuppressWarnings("deprecation")
	protected void doProcess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<RetailProduct> item = new ArrayList<RetailProduct>();
		
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail",
					"sa", "");
			PreparedStatement ps = con.prepareStatement("SELECT * FROM PRODUCT");
		   
			ResultSet rs = ps.executeQuery();
			
		
			while (rs.next()) {
				RetailProduct product = new RetailProduct();
				product.setRProductID(rs.getString("PRODUCTID"));
				product.setRProductPrice(rs.getBigDecimal("SOLD_PRICE"));
				product.setRProductCost(rs.getBigDecimal("COST_PRICE"));
				product.setRProductQuantity(rs.getInt("QUANTITY"));
				product.setRProductVaildDate(rs.getInt("VALID_DATE"));
				product.setRProductEntryDate(rs.getDate("WAREHOUSING_TIME"));
				item.add(product);
			} 
			System.out.println(item);
			ps.close();
			con.close();
			
		        JasperPrint jasperPrint = null;  
		        
		        try {  
		        	JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream("report3.jrxml")); 
		            jasperPrint = JasperFillManager.fillReport(jasperReport,null,  
		                    new JRBeanCollectionDataSource(item));  
		        } catch (JRException e) {  
		            e.printStackTrace();  
		        }  

		        
		        resp.setContentType("application/pdf");
		        OutputStream outStream = resp.getOutputStream();
		        try {
					JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
				} catch (JRException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		        
		        
			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
		
		
		
		
		
		
	}
	
	
